{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d3ecf032",
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "import os\n",
    "import soundfile as sf\n",
    "import librosa\n",
    "from tqdm import tqdm\n",
    "from multiprocess import Pool\n",
    "import itertools\n",
    "\n",
    "def chunks(l, n):\n",
    "    for i in range(0, len(l), n):\n",
    "        yield (l[i: i + n], i // n)\n",
    "\n",
    "def multiprocessing(strings, function, cores=6, returned=True):\n",
    "    df_split = chunks(strings, len(strings) // cores)\n",
    "    pool = Pool(cores)\n",
    "    pooled = pool.map(function, df_split)\n",
    "    pool.close()\n",
    "    pool.join()\n",
    "\n",
    "    if returned:\n",
    "        return list(itertools.chain(*pooled))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "626434c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35000"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "files = glob('Emotion Speech Dataset/*/*/*.wav')\n",
    "len(files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a60dcb04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"['Neutral', 'Surprise', 'Sad', 'Angry', 'Happy']\""
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "emotions = list(set([f.split('/')[-2] for f in files]))\n",
    "emotions = str(emotions)\n",
    "emotions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5d132b40",
   "metadata": {},
   "outputs": [],
   "source": [
    "!mkdir esd-emotion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3a7d2b36",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "questions = [\n",
    "    'given the labels\\n{labels}\\n\\nclassify the audio',\n",
    "    'what is the label for audio\\n\\nthe labels: {labels}'\n",
    "]\n",
    "\n",
    "def loop(files):\n",
    "    files, _ = files\n",
    "    data = []\n",
    "    for f in tqdm(files):\n",
    "        y, sr = librosa.load(f)\n",
    "        label = f.split('/')[-2]\n",
    "        splitted = '_'.join(f.split('/')[-2:]).replace('.wav', '.mp3')\n",
    "        audio_filename = os.path.join('esd-emotion', splitted)\n",
    "        if not os.path.exists(audio_filename):\n",
    "            sf.write(audio_filename, y, sr)\n",
    "        \n",
    "        data.append({\n",
    "            'question': random.choice(questions).format(labels = emotions),\n",
    "            'answer': label,\n",
    "            'metadata': '',\n",
    "            'audio_filename': audio_filename,\n",
    "        })\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "82ae06c1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|                                                                                              | 0/1750 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "  0%|                                                                                              | 0/1750 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "  0%|                                                                                              | 0/1750 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "  0%|                                                                                              | 0/1750 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "  0%|                                                                                              | 0/1750 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:03<00:00, 493.63it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:03<00:00, 478.55it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:04<00:00, 432.34it/s]\n",
      " 75%|█████████████████████████████████████████████████████████████▊                    | 1319/1750 [00:04<00:00, 813.67it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:04<00:00, 381.16it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:04<00:00, 369.55it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:04<00:00, 355.00it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:05<00:00, 345.63it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:04<00:00, 356.31it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:05<00:00, 347.10it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:05<00:00, 349.44it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:05<00:00, 349.06it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:04<00:00, 352.72it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:05<00:00, 338.96it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:04<00:00, 357.32it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:05<00:00, 348.83it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:04<00:00, 356.52it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:05<00:00, 330.00it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:05<00:00, 335.96it/s]\n",
      "100%|██████████████████████████████████████████████████████████████████████████████████| 1750/1750 [00:05<00:00, 324.10it/s]\n"
     ]
    }
   ],
   "source": [
    "processed = multiprocessing(files, loop, cores = 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9d166626",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets import Dataset\n",
    "\n",
    "dataset = Dataset.from_list(processed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0de02b37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7a22c2a2b43b43438a51a428e469c917",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "61425d8f63434b7da6872fdd4bd8b576",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/35 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "64c4add032d24553bcfd86ec8f6141a2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading...:   0%|          | 0.00/285k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "61e437cb0b2b4d16b9cb108a1725439c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/3.56k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions/commit/59de091202185bb75163cc0d7ed8c252f7202119', commit_message='Upload dataset', commit_description='', oid='59de091202185bb75163cc0d7ed8c252f7202119', pr_url=None, repo_url=RepoUrl('https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions', endpoint='https://huggingface.co', repo_type='dataset', repo_id='mesolitica/Zeroshot-Audio-Classification-Instructions'), pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.push_to_hub('mesolitica/Zeroshot-Audio-Classification-Instructions', split = 'esd_emotion')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1487707a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35000"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from glob import glob\n",
    "\n",
    "audio_files = glob('esd-emotion/*.mp3')\n",
    "len(audio_files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1dccd7f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "662M\tesd-emotion\r\n"
     ]
    }
   ],
   "source": [
    "!du -hs esd-emotion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "298771ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "import zipfile\n",
    "\n",
    "with zipfile.ZipFile('esd-emotion.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:\n",
    "    for f in audio_files:\n",
    "        zipf.write(f, arcname=f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3ade10cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uploading files using Xet Storage..\n",
      "Uploading...: 100%|██████████████████████████| 610M/610M [00:25<00:00, 24.0MB/s]\n",
      "https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions/blob/main/esd-emotion.zip\n"
     ]
    }
   ],
   "source": [
    "!huggingface-cli upload mesolitica/Zeroshot-Audio-Classification-Instructions esd-emotion.zip \\\n",
    "--repo-type=dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ff8de622",
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -rf 'Emotion Speech Dataset'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
